# -*- coding: utf-8 -*-
# Project  : JXC
# FileName : TimeHandler.py
# Time     : 2020/7/20 14:56
# Author  : Mason101

import calendar
from datetime import datetime, timedelta


class DateTimeHandler:
    @staticmethod
    def today():
        return datetime.now().strftime("%Y-%m-%d")

    @staticmethod
    def day_start_end(times=None):
        times = datetime.now() if times is None else datetime.strptime(times, "%Y-%m-%d")
        start = times.strftime("%Y-%m-%d 00:00:00")
        end = times.strftime("%Y-%m-%d 23:59:59")
        return start, end

    # 昨天
    @staticmethod
    def get_last_day(times=None):
        last_date = datetime.now() if not times else datetime.strptime(times, "%Y-%m-%d")
        last_date = last_date - timedelta(days=1)
        return last_date.strftime("%Y-%m-%d")

    def some_days_time(self, days=7):
        dates = []
        for i in range(days):
            if len(dates) == 0:
                today = datetime.today()
                dates.append(today.strftime("%Y-%m-%d"))
            else:
                dates.append(self.get_last_day(dates[-1]))
        print(dates)
        return dates

    # 上个月
    @staticmethod
    def get_last_month(times=None):
        times = datetime.now() if times is None else datetime.strptime(times, "%Y-%m-%d")
        first_day = times.replace(day=1)
        last_month = first_day - timedelta(days=1)
        return last_month.strftime("%Y-%m-%d")

    @staticmethod
    def month_start_end(times=None):
        times = datetime.now() if times is None else datetime.strptime(times, "%Y-%m-%d")
        month_begin = times.strftime("%Y-%m-01")
        wday, mrange = calendar.monthrange(times.year, times.month)
        print(wday, mrange)
        month_end = "%d-%02d-%02d" % (times.year, times.month, mrange)
        return month_begin, month_end

    @staticmethod
    def times_range_by_day(start, end):
        times = datetime.strptime(start, "%Y-%m-%d")
        dates = []
        for i in range(31):
            t = "%d-%02d-%02d" % (times.year, times.month, i+1)
            dates.append(t)
            if t == end:
                break
        return dates


if __name__ == '__main__':
    start, end = DateTimeHandler().month_start_end(DateTimeHandler().get_last_month())
    result = DateTimeHandler().times_range_by_day(start, end)
    print(result)
